package totask.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import totask.dao.TaskMapper;
import totask.pojo.TotaskTask;
import totask.service.TaskService;
import totask.vo.TotaskTaskVo;

import java.util.*;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;


    @Override
    public List<TotaskTask> queryTaskAll(Integer ProjectId) {
        QueryWrapper<TotaskTask> query = new QueryWrapper();
        query.eq("project_id", ProjectId);
        List<TotaskTask> totasktasks = taskMapper.selectList(query);
        return totasktasks;
    }

    @Override
    public String insertTask(TotaskTask task) {
        System.out.println(DateUtil.parse(task.getTaskstarttime().toString()));
        task.setTaskstarttime(DateUtil.parse(task.getTaskstarttime().toString()));
        int insertTaskFlag = taskMapper.insert(task);
        if (insertTaskFlag > 0) {
            return "新增成功";
        } else {
            return "新增失败";
        }
    }

    @Override
    public String updateTask(TotaskTask task) {
        TotaskTask totaskTask = new TotaskTask();
        totaskTask.setTaskId(task.getTaskId());
        totaskTask.setTaskname(task.getTaskname());
        totaskTask.setTaskdescribe(task.getTaskdescribe());
        totaskTask.setDevid(task.getDevid());
        totaskTask.setTestid(task.getTestid());
        totaskTask.setTaskstarttime(task.getTaskstarttime());
        totaskTask.setTaskendtime(task.getTaskendtime());
        totaskTask.setTaskstatusid(task.getTaskstatusid());
        int updateById = taskMapper.updateById(totaskTask);
        if (updateById > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @Override
    public TotaskTaskVo queryUserTaskAll(String userId) {
        //查询项目id
        List<TotaskTask> totaskTasks;
        //TotaskTaskset存储任务项目id
        Set<Integer> TotaskTaskset = new HashSet();
        QueryWrapper<TotaskTask> query1 = new QueryWrapper();
        query1.eq("devid", userId).or().eq("testid", userId);
        totaskTasks = taskMapper.selectList(query1);
        totaskTasks.forEach(item -> TotaskTaskset.add(item.getProjectId()));

        return new TotaskTaskVo(totaskTasks,TotaskTaskset);
    }

//    @Override
//    public TotaskTask queryByTask(Integer ProjectId) {
//        QueryWrapper<TotaskTask> query = new QueryWrapper();
//        query.eq("project_id", ProjectId);
//        TotaskTask totaskTask = taskMapper.selectOne(query);
//        return totaskTask;
//    }

    @Override
    public Map<String, Integer> queryProjectProgress(Integer projectId) {
        Map<String, Integer> map = new HashMap<String, Integer>();

        QueryWrapper<TotaskTask> query = new QueryWrapper();
        query.eq("project_id", projectId);
        Integer TotaskTaskProgress1 = Math.toIntExact(taskMapper.selectCount(query));
        map.put("ProjectAll", TotaskTaskProgress1);
        query.eq("taskstatusid", 4);
        Integer TotaskTaskProgress2 = Math.toIntExact(taskMapper.selectCount(query));
        map.put("ProjectProgress", TotaskTaskProgress2);
        return map;
    }

    @Override
    public Map<String, Integer> queryTaskProgress(Integer projectId, String userId,Integer roleId) {
        Integer TotaskTaskProgress1=0;
        Integer TotaskTaskProgress2=0;
        Map<String, Integer> map = new HashMap();

        QueryWrapper<TotaskTask> query = new QueryWrapper();
        query.eq("project_id", projectId);
        //完成进度
        if(roleId==4||roleId==2){  //开发人员
            query.eq("devid", userId); //我的任务
             TotaskTaskProgress1 = Math.toIntExact(taskMapper.selectCount(query));
            map.put("TaskAllProgress", TotaskTaskProgress1);
            query.and(Wrapper -> Wrapper.eq("taskstatusid",3).or().eq("taskstatusid",4));
             TotaskTaskProgress2 = Math.toIntExact(taskMapper.selectCount(query));
            map.put("TaskProgress", TotaskTaskProgress2);
        }else if (roleId==5){   //测试人员
            query.eq("testid", userId); //我的任务
            TotaskTaskProgress1 = Math.toIntExact(taskMapper.selectCount(query));
            map.put("TaskAllProgress", TotaskTaskProgress1);
            query.eq("taskstatusid", 4);
            TotaskTaskProgress2 = Math.toIntExact(taskMapper.selectCount(query));
            map.put("TaskProgress", TotaskTaskProgress2);
        }
        return map;
    }
}
